<template>
  <el-form ref="form" :model="form" label-width="120px" :visible="isAdd">
    <el-row>
      <el-col :span="24">
        <el-form-item label="景区名称">
          <el-input v-model="form.name" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="地区">
          <el-select v-model="form.areaId" placeholder="请选择地区">
            <template v-for="item in areas">
              <el-option :key="item.areaId" :label="item.areaName" :value="item.areaId" />
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="景区类别">
          <el-select v-model="form.attrCateId" placeholder="请选择类别">
            <el-option v-for="item in attrTypes" :key="item.cateId" :label="item.cateName" :value="item.cateId" />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="8">
        <el-form-item label="开放时间">
          <el-input v-model="form.openTime" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="价格">
          <el-input-number v-model="form.price" :pricision="2" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="限制人数">
          <el-input v-model="form.limitPerson" type="number" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-form-item label="景区封面">
      <!-- <el-image :src="form.bannerUrl" style="height: 300px" fit="contain">
        <div slot="error" class="image-slot">
          <i class="el-icon-picture-outline" />
        </div>
      </el-image> -->
      <el-upload
        ref="uploadFile"
        class="upload-demo"
        name="file"
        :show-file-list="false"
        :before-upload="beforeUpload"
        :on-change="handleChange"
        :headers="headerObj"
        drag
        :action="uploadUrl"
        :on-success="handleImageSuccess"
        with-credentials
      >
        <el-image v-if="form.bannerUrl" :src="form.bannerUrl" style="height: 300px" fit="cover">
          <div slot="error" class="image-slot">
            <i class="el-icon-picture-outline" />
          </div>
        </el-image>
        <i v-else class="el-icon-upload" />
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png/jepg文件，且不超过2M</div>
      </el-upload>
    </el-form-item>
    <el-form-item label="详细地址">
      <el-input v-model="form.address" />
    </el-form-item>
    <el-form-item label="购票须知">
      <el-input v-model="form.ticketNotice" type="textarea" />
    </el-form-item>
    <el-form-item label="景区简介">
      <el-input v-model="form.attrDesc" type="textarea" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">保存</el-button>
      <el-button @click="onCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { addAttr } from '@/api/travel'
import { mapGetters } from 'vuex'
import { validateImage } from '@/utils/validate'

export default {
  name: 'AddAttraction',
  data() {
    return {
      form: {
        bannerUrl: null
      },
      uploadUrl: process.env.VUE_APP_BASE_API + '/travel/updateAttrBanner',
      headerObj: ''
    }
  },
  computed: {
    ...mapGetters([
      'areas',
      'attrTypes',
      'attrId',
      'attraction',
      'token'
    ])
  },

  created() {
    console.log(this.isAdd)
    this.headerObj = {
      'token': this.token
    }
  },
  methods: {
    onCancel() {
      this.$emit('update:isAdd', false)
    },
    onSubmit(e) {
      console.log(this.form)
      addAttr(this.form).then(res => {
        if (res.code === 200) {
          this.$message.success(res.message)
          this.$router.replace('/travel/index')
          this.onCancel()
          this.form = {}
        } else {
          this.message.error(res.message)
        }
      })
    },
    handleImageSuccess(res, file) {
      console.log(res)
      if (res.code === 200) {
        const { bannerUrl } = res.data
        this.form.bannerUrl = bannerUrl
      } else {
        this.$message.error(res.message)
      }
    },
    handleChange(file) {
      console.log(file)
      console.log(this.$refs.uploadFile.uploadFiles[0])
    },
    beforeUpload(file) {
      console.log(file)
      validateImage(file)
    }
  }
}
</script>

